package org.lex.app.sessions;

import org.lex.utils.collections.ArrayTable;

public abstract class AbstractSession implements Session {
	private ArrayTable table = new ArrayTable();

	public void clear() {
		table.clear();
	}

	@Override
	public boolean containsProperty(String property) {
		return table.containsKey(property);
	}

	@Override
	public String getProperty(String property) {
		return (String) table.get(property);
	}

	@Override
	public String[] propertyNames() {
		Object[] tmp = table.getKeys(null);
		String[] keys = new String[tmp.length];
		for (int i = 0; i < tmp.length; i++)
			keys[i] = (String) tmp[i];
		return keys;
	}

	public void putProperty(String property, String value) {
		table.put(property, value);
	}

	protected int getInt(String key, int defaultValue) {
		String v = this.getProperty(key);
		if (null == v)
			return defaultValue;
		else
			return Integer.parseInt(v);
	}

	protected void putInt(String key, int value) {
		this.putProperty(key, Integer.toString(value));
	}

	public String toString() {
		StringBuilder buf = new StringBuilder();
		String[] properties = this.propertyNames();
		String sep = ", ";
		for (String p : properties)
			buf.append(p).append('=').append(this.getProperty(p)).append(sep);
		if (buf.length() > sep.length())
			buf.setLength(buf.length() - sep.length());
		return buf.toString();
	}
}
